import requests
from lxml import etree

# 双色球数据
url = 'https://datachart.500.com/ssq/'
headers = {
    # ':authority': 'datachart.500.com',
    # ':method': 'GET',
    # ':path': '/ssq/',
    # ':scheme': 'https',
    # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    # 'Accept-Encoding': 'gzip, deflate, br, zstd',
    # 'Accept-Language': 'zh-CN,zh;q=0.9',
    # 'Cache-Control': 'max-age=0',
    # 'Connection': 'keep-alive',
    # 'Cookie': 'WT_FPC=id=undefined:lv=1744860343862:ss=1744860343862; sdc_session=1744860343863; sdc_userflag=1744860343863::1744860343863::1; _jzqa=1.1740964683385255000.1744860344.1744860344.1744860344.1; _jzqc=1; _jzqckmp=1; Hm_lvt_4f816d475bb0b9ed640ae412d6b42cab=1744860344; Hm_lpvt_4f816d475bb0b9ed640ae412d6b42cab=1744860344; HMACCOUNT=80BCC7E24F98D420; _jzqb=1.1.10.1744860344.1; _qzja=1.1535760803.1744860344153.1744860344153.1744860344153.1744860344153.1744860344153.0.0.0.1.1; _qzjc=1; _qzjto=1.1.0; _qzjb=1.1744860344153.1.0.0.0; __utma=63332592.1997885291.1744860345.1744860345.1744860345.1; __utmc=63332592; __utmz=63332592.1744860345.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; __utmb=63332592.1.10.1744860345; motion_id=1744860352964_0.46359416867219394; CLICKSTRN_ID=240e:332:1161:cead:9dce:991c:87c1:86e9-1744860343.9465759::756401F9B2F9AA5D463DDFCB2E757967',
    # 'Priority': 'u=0, i',
    # 'Sec-Ch-Ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
    # 'Sec-Ch-Ua-Mobile': '?0',
    # 'Sec-Ch-Ua-Platform': '"Windows"',
    # 'Sec-Fetch-Dest': 'document',
    # 'Sec-Fetch-Mode': 'navigate',
    # 'Sec-Fetch-Site': 'none',
    # 'Sec-Fetch-User': '?1',
    # 'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
}
resp = requests.get(url, headers=headers)
resp.encoding = 'gbk'
# print(resp.text)
e = etree.HTML(resp.text)

nums = e.xpath('//tbody[@id="tdata"]/tr/td[@align="center"]/text()')
reds = [tr.xpath('./td[contains(@class,"chartBall01")]/text()') for tr in
        e.xpath('//tbody[@id="tdata"]/tr[not(contains(@class,"tdbck"))]')]
blues = e.xpath('//tbody[@id="tdata"]/tr[not(contains(@class,"tdbck"))]/td[contains(@class,"chartBall02")]/text()')
data_list = []
for r, b, num in zip(reds, blues, nums):
    # print(f"\t红球是：{r} \t蓝球是：{b}")
    data_list.append({
        'red': r,
        'blue': b,
        'num': num
    })
print(data_list)
